home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / hamradio / ftp4w.zip / sample / main.c < prev    next >
C/C++ Source or Header  |  1994-11-15  |  18KB  |  544 lines

  1. /* **************************************************************
  2.  *
  3.  *
  4.  *      C W _ M A I N 
  5.  * 
  6.  *
  7.  * **************************************************************** */
  8.  
  9. #define  STRICT
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "..\ftp4w.h"
  15. #include "resource.h"
  16.  
  17.  
  18. /* user's defined messages */
  19. #define  FTP_LOGGED          (WM_USER + 48)
  20. #define  FTP_DIR             (WM_USER + 51)
  21. #define  FTP_FILETRANSFERED  (WM_USER + 54)
  22. #define  CW_VERBOSE          (WM_USER + 57)
  23. #define  CW_BEGIN            (WM_USER + 60)
  24. #define  SHORT_DIR            FALSE
  25. #define  IsOptOn(x) (GetMenuState (GetMenu(hWnd),x,MF_BYCOMMAND)==MF_CHECKED)
  26.  
  27.  
  28.  
  29. int  nHorzSiz, nVertSiz;
  30. int  nVertPos, nHorzPos;           // scroll pos for debug window
  31. int  nHorzMax, nCurMsg;
  32. int  nMaxMsg;
  33. int  nHorzTab = 20;
  34.  
  35.  
  36. char            szApplication[20]; /* name of application          */
  37. HINSTANCE       hInst;             /* hInstance of application     */
  38. HWND            hwnd;              /* hWnd of main window          */
  39.  
  40. BOOL            FirstEmission;
  41. BOOL            bFileTransfer=FALSE; /* TRUE->data transfer in progress */
  42.  
  43.  
  44. /* in main2 : gauge window */
  45. void SetXmitBytes (LONG lBytes, LONG lTotalBytes);
  46. void DeleteXferWindow (void);
  47. void CreateXferWindow(void);
  48. void SetXferWindowText (LPSTR lpStr);
  49. int nCwRegisterClasses(void);
  50. int nCwUnregisterClasses(void);
  51. /* in main2 : scroller window */
  52. int  Ecris(const char *szFormat,...);
  53. void DoPrintf (char * szFormat, ...);
  54. void DoPaint (HWND hwnd);
  55. void DoAddLine (LPSTR szLine);                           
  56. void ReleaseDisplayMem(void);
  57.  
  58. /* in this file */
  59. int PASCAL FAR InitWindow (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  60.                            LPSTR lpszCmdLine, int cmdShow);
  61.  
  62.  
  63.  
  64. // #define  WM_FTPDIR  (WM_USER+2)
  65.  
  66.  
  67. LPSTR HOST_NAME(void) 
  68. {
  69. static char szBuf[256];
  70.  GetPrivateProfileString ("Connect", "Host", "", szBuf,256, "cw_main.ini");
  71.   if (szBuf[0]==0)
  72.     {
  73.       MessageBox ( NULL,
  74.                    "Field Host not defined in Cw_Main.Ini", 
  75.                    "CW_MAIN", MB_OK);
  76.       return NULL;
  77.     }
  78.   return szBuf; }  
  79.                         
  80.  
  81. LPSTR USER_NAME(void) 
  82. {
  83. static char szBuf[256];
  84.  GetPrivateProfileString ("Connect", "User", "", szBuf,256, "cw_main.ini");
  85.   if (szBuf[0]==0)
  86.     {
  87.       MessageBox ( NULL,
  88.                    "Field User not defined in Cw_Main.Ini", 
  89.                    "CW_MAIN", MB_OK);
  90.       return NULL;
  91.     }
  92.   return szBuf; }  
  93.  
  94.  
  95. LPSTR PASSWD_NAME(void) 
  96. {
  97. static char szBuf[256];
  98.  GetPrivateProfileString ("Connect", "PassWd", "", szBuf,256, "cw_main.ini");
  99.   if (szBuf[0]==0)
  100.     {
  101.       MessageBox ( NULL,
  102.                    "Field PassWd not defined in Cw_Main.Ini", 
  103.                    "CW_MAIN", MB_OK);
  104.       return NULL;
  105.     }
  106.   return szBuf; }  
  107.                         
  108. LPSTR LOCALFILE_NAME(void) 
  109. {
  110. static char szBuf[256];
  111.  GetPrivateProfileString ("Transfer", "LocalFile", "",szBuf,256,"cw_main.ini");
  112.   if (szBuf[0]==0)
  113.     {
  114.       MessageBox ( NULL,
  115.                    "Field LocalFile not defined in Cw_Main.Ini", 
  116.                    "CW_MAIN", MB_OK);
  117.       return NULL;
  118.     }
  119.   return szBuf; }  
  120.                         
  121.                         
  122. LPSTR REMOTEFILE_NAME(void) 
  123. {
  124. static char szBuf[256];
  125.  GetPrivateProfileString ("Transfer", "RemoteFile", "",szBuf,256,"cw_main.ini");
  126.   if (szBuf[0]==0)
  127.     {
  128.       MessageBox ( NULL,
  129.                    "Field RemoteFile not defined in Cw_Main.Ini", 
  130.                    "CW_MAIN", MB_OK);
  131.       return NULL;
  132.     }
  133.   return szBuf; }  
  134.  
  135.  
  136. long FileSize (LPSTR szFile)
  137. {
  138. HFILE hF;
  139. long FS;
  140.   hF = _lopen (szFile, 0);
  141.   if (hF==HFILE_ERROR)  return 0;
  142.   FS = _llseek (hF, 0, SEEK_END);
  143.   _lclose (hF);
  144. return FS;
  145. } /* FileSize */
  146.  
  147.  
  148. //*******************************************************************
  149. LRESULT CALLBACK   MainWndProc (HWND hWnd, UINT message,
  150.                                 WPARAM wParam, LPARAM lParam)
  151. {
  152. static char szBuf[10240];
  153. int         Rc;
  154. LPSTR       p, q;
  155. HFILE       hF;
  156.  
  157.     switch (message)
  158.     {
  159.        case WM_CREATE :
  160.           SetScrollPos (hWnd, SB_HORZ, 0, TRUE);
  161.           SetScrollPos (hWnd, SB_VERT, 0, TRUE);
  162.           SetScrollRange (hWnd, SB_HORZ, 0, 1, TRUE);
  163.           SetScrollRange (hWnd, SB_VERT, 0, 1, TRUE);
  164.           nVertPos = nHorzPos = nHorzMax = nCurMsg = 0;
  165.           nMaxMsg = 80;
  166.           nCwRegisterClasses();
  167.           PostMessage (hWnd, CW_BEGIN, 0, 0);
  168.           break;  
  169.  
  170.  
  171.  
  172.        case WM_VSCROLL:
  173.           switch(wParam)
  174.            {
  175.             case SB_LINEDOWN     : if (nVertPos<(nCurMsg-(nVertSiz>>1))) nVertPos++;  break;
  176.             case SB_LINEUP       : if (nVertPos>0) --nVertPos; break;
  177.             case SB_THUMBPOSITION: nVertPos = min ((WORD) (nCurMsg-(nVertSiz>>1)), LOWORD (lParam)); break;
  178.             case SB_PAGEUP       : nVertPos = (nVertPos>10) ? (nVertPos-10) : 0; break;
  179.             case SB_PAGEDOWN     : nVertPos = (nVertPos<(nCurMsg-nVertSiz)) ? (nVertPos+nVertSiz) : nCurMsg-(nVertSiz>>1); break;
  180.             default              : return 0L;
  181.            } /* switch wParam */
  182.           SetScrollPos (hWnd, SB_VERT, nVertPos, TRUE);
  183.           InvalidateRect (hWnd, NULL, TRUE); 
  184.           return 0L;
  185.  
  186.        case WM_HSCROLL:
  187.           FtpAbort ();
  188.           switch (wParam)
  189.            {
  190.             case SB_LINEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzTab)) ? (nHorzPos+nHorzTab) : nHorzMax; break;
  191.             case SB_LINEUP       : nHorzPos = (nHorzPos>nHorzTab) ? (nHorzPos-nHorzTab) : 0; break;
  192.             case SB_THUMBPOSITION: nHorzPos = min ((WORD) nHorzMax, LOWORD (lParam)); break;
  193.             case SB_PAGEUP       : nHorzPos = (nHorzPos>nHorzSiz) ? (nHorzPos-nHorzSiz) : 0; break;
  194.             case SB_PAGEDOWN     : nHorzPos = (nHorzPos<(nHorzMax-nHorzSiz)) ? (nHorzPos+nHorzSiz) : nHorzMax; break;
  195.             default              : return 0L;
  196.            }
  197.           SetScrollPos (hWnd, SB_HORZ, nHorzPos, TRUE);
  198.           InvalidateRect (hWnd, NULL, TRUE); 
  199.           return 0L;
  200.  
  201.       case WM_PAINT :
  202.           DoPaint (hWnd);
  203.           break;
  204.           
  205.    /* ---------------------------------------------------------- */
  206.  
  207.        case CW_BEGIN :
  208.  DoAddLine ("----------------------------------------------------------------");
  209.           DoAddLine ("Sample program For FTP4W.DLL By Ph. Jounin");
  210.           DoAddLine ("Host description and file names must be defined");
  211.           DoAddLine ("in the file CW_MAIN.INI as shown in the INI file");
  212.           DoAddLine ("provided in the package FTP4W.ZIP.");
  213.           DoAddLine (" ");
  214.           DoAddLine ("Please report bugs and disfonctionments to");
  215.           DoAddLine ("           ark@ifh.sncf.fr");
  216.  DoAddLine ("----------------------------------------------------------------");
  217.  
  218.           /* initialize FTP sesson */  
  219.           if (FtpInit(NULL) != FTPERR_OK)   Ecris ("FtpInit failed !");
  220.           else
  221.              {
  222.                 FtpSetDefaultTimeOut (30);       /* new Timeout : 30 seconds */
  223.              }
  224.           break;
  225.  
  226.  
  227.       case WM_CLOSE : 
  228.           if (! bFileTransfer  ||
  229.               MessageBox (NULL, 
  230.                           "Abort transfer", 
  231.                           "CW_MAIN", MB_ICONQUESTION |
  232.                            MB_OKCANCEL) == IDOK )
  233.                   {
  234.                      FtpLocalClose ();
  235.                      FtpRelease ();
  236.                      ReleaseDisplayMem();
  237.                      PostQuitMessage (0);
  238.                   }  
  239.                 return 0;
  240.            break;  
  241.  
  242.       case WM_QUERYENDSESSION :
  243.           FtpLocalClose ();
  244.           FtpRelease ();
  245.           ReleaseDisplayMem();
  246.           DeleteXferWindow ();
  247.           break;
  248.  
  249.       /* --------------------- */
  250.       /* asynchronous Messages */
  251.       /* --------------------- */
  252.       case CW_VERBOSE :
  253.           DoAddLine ((LPSTR) lParam);
  254.           break;
  255.  
  256.       case FTP_LOGGED :
  257.           Ecris ("Asynchronous Login returns %d", (int) lParam);
  258.           break;
  259.  
  260.       case FTP_DIR :
  261.           Ecris ("Dir returns %d", (int) lParam);
  262.           Ecris ("-------");
  263.           hF = _lopen ("$$dir$$.tmp", 0);
  264.           _lread (hF, szBuf, sizeof szBuf);
  265.           _lclose (hF);
  266.           for (p=szBuf ; (q=strchr (p,'\r')) !=NULL ; p=q+2 )
  267.               {  *q=0;
  268.                   DoAddLine (p); }
  269.           Ecris ("-------");
  270.           unlink ("$$dir$$.tmp");
  271.           break;
  272.  
  273.  
  274.        case FTP_FILETRANSFERED :
  275.           if (wParam)
  276.             {
  277.                DeleteXferWindow ();
  278.                Ecris ("Asynchronous transfer returns %d", (int) lParam);
  279.                bFileTransfer = FALSE;
  280.             }
  281.           else
  282.             {
  283.                SetXmitBytes (lParam, FtpBytesToBeTransfered ());
  284.             }  
  285.           break;
  286.                 
  287.  
  288.       /* --------------------- */
  289.       /*     M   E   N   U     */
  290.       /* --------------------- */
  291.       case  WM_COMMAND :
  292.         switch (wParam)
  293.          {
  294.  
  295.            case  CW_CONNECT :
  296.                Ecris ("--- Connection on %s  ---", HOST_NAME () );
  297.                Rc = FtpLogin ( HOST_NAME (), 
  298.                                USER_NAME (), 
  299.                                PASSWD_NAME (),
  300.                                hWnd, FTP_LOGGED);
  301.                Ecris ("Function returns %d", Rc);                              
  302.                break;
  303.  
  304.  
  305.            case CW_RECV :
  306.                Ecris ("Remote %s -> Local %s",REMOTEFILE_NAME (),LOCALFILE_NAME ());
  307.                bFileTransfer = TRUE;
  308.                if (IsOptOn(CW_GAUGE))
  309.                  {
  310.                     CreateXferWindow ();
  311.                     SetXferWindowText (LOCALFILE_NAME());
  312.                  }
  313.                Rc = FtpRecvFile ( REMOTEFILE_NAME (),
  314.                                   LOCALFILE_NAME (),
  315.                                   IsOptOn(CW_BINARY) ? TYPE_I : TYPE_A, 
  316.                                   IsOptOn(CW_GAUGE),
  317.                                   hWnd, 
  318.                                   FTP_FILETRANSFERED);
  319.                if (! IsOptOn (CW_SYNC) )
  320.                  {       
  321.                     Ecris ("Taille %ld", FtpBytesToBeTransfered () );
  322.                  }
  323.                else
  324.                 {
  325.                     DeleteXferWindow ();
  326.                     Ecris ("Recv returns %d", Rc);
  327.                     bFileTransfer = FALSE;
  328.                 }
  329.                break;
  330.  
  331.         
  332.  
  333.            case CW_SEND :
  334.                Ecris ("Local %s -> Remote %s",LOCALFILE_NAME (),REMOTEFILE_NAME ());
  335.                bFileTransfer = TRUE;
  336.                if (IsOptOn(CW_GAUGE))
  337.                  {
  338.                     CreateXferWindow ();
  339.                     SetXferWindowText (LOCALFILE_NAME());
  340.                  }
  341.                Rc = FtpSendFile ( LOCALFILE_NAME (),
  342.                                   REMOTEFILE_NAME (),
  343.                                   IsOptOn(CW_BINARY) ? TYPE_I : TYPE_A, 
  344.                                   IsOptOn(CW_GAUGE),
  345.                                   hWnd, 
  346.                                   FTP_FILETRANSFERED);
  347.                if (! IsOptOn (CW_SYNC) )
  348.                  {       
  349.                     Ecris ("Taille %ld", FtpBytesToBeTransfered () );
  350.                  }
  351.                else
  352.                  {
  353.                     DeleteXferWindow ();
  354.                     Ecris ("Send returns %d", Rc);
  355.                     bFileTransfer = FALSE;
  356.                  }
  357.                break;
  358.  
  359.  
  360.            case CW_SHORTDIR :
  361.            case CW_LONGDIR :
  362.                Rc=FtpDir (NULL, "$$dir$$.tmp",wParam==CW_LONGDIR,hWnd,FTP_DIR);
  363.                if (IsOptOn(CW_SYNC))  PostMessage (hWnd, FTP_DIR, TRUE, Rc);
  364.                break;
  365.  
  366.  
  367.            case CW_ABORT :
  368.                if (! bFileTransfer)
  369.                  {
  370.                    Ecris ("No active file transfer");
  371.                  }
  372.                else
  373.                  {
  374.                     Ecris ("Abort");
  375.                     FtpAbort ();
  376.                  }
  377.                break;  
  378.  
  379.  
  380.            case CW_DISCONNECT : 
  381.                if (bFileTransfer)
  382.                   {
  383.                      Ecris ("File transfer in progress, can not close session");
  384.                   }
  385.                else
  386.                   {
  387.                      Rc = FtpCloseConnection ();
  388.                      Ecris ("FtpCloseConnection returns %d", Rc);
  389.                   }
  390.                break;
  391.  
  392.  
  393.            case CW_QUIT : 
  394.                if (! bFileTransfer  ||
  395.                      MessageBox (NULL, 
  396.                                 "Abort transfer", 
  397.                                 "CW_MAIN", MB_ICONQUESTION |
  398.                                  MB_OKCANCEL) == IDOK )
  399.                      {
  400.                         FtpLocalClose ();
  401.                         ReleaseDisplayMem();
  402.                         FtpRelease ();
  403.                         PostQuitMessage (0);
  404.                       }  
  405.                      return 0;
  406.                      
  407.            case CW_SYNC : 
  408.                if (bFileTransfer)
  409.                  {
  410.                    MessageBox (NULL, "Can not change now", "CW_MAIN", MB_OK);
  411.                  }
  412.                else
  413.                  {
  414.                    if (! IsOptOn (CW_SYNC))
  415.                       {
  416.                           CheckMenuItem (GetMenu (hWnd), CW_SYNC, 
  417.                                          MF_BYCOMMAND  | MF_CHECKED);
  418.                           FtpSetSynchronousMode ();
  419.                        }
  420.                    else   
  421.                       {
  422.                           CheckMenuItem (GetMenu (hWnd), CW_SYNC, 
  423.                                          MF_BYCOMMAND  | MF_UNCHECKED);
  424.                           FtpSetAsynchronousMode ();
  425.                        }
  426.                  }
  427.                break;      
  428.                      
  429.            case CW_SETVERB :
  430.                if (IsOptOn (CW_SETVERB))
  431.                    
  432.                  {                
  433.                    CheckMenuItem (GetMenu (hWnd), CW_SETVERB, 
  434.                                   MF_BYCOMMAND  | MF_UNCHECKED);
  435.                    FtpSetVerboseMode (FALSE, hWnd, CW_VERBOSE);
  436.                  }
  437.                else
  438.                  {                
  439.                    CheckMenuItem (GetMenu (hWnd), CW_SETVERB, 
  440.                                   MF_BYCOMMAND  | MF_CHECKED);
  441.                    FtpSetVerboseMode (TRUE, hWnd, CW_VERBOSE);
  442.                  }
  443.                break; 
  444.  
  445.  
  446.            case CW_GAUGE :
  447.                CheckMenuItem (GetMenu (hWnd), CW_GAUGE, MF_BYCOMMAND  | 
  448.                               IsOptOn(CW_GAUGE) ? MF_UNCHECKED : MF_CHECKED); 
  449.                /* pause (for display) each 20 frames if gauge is on */
  450.                if IsOptOn(CW_GAUGE)   FtpSetNewSlices(20, 5);
  451.                else                   FtpSetNewSlices(50, 15);
  452.                break; 
  453.                 
  454.                 
  455.            case CW_BINARY :
  456.                CheckMenuItem (GetMenu (hWnd), CW_BINARY, MF_BYCOMMAND  | 
  457.                               IsOptOn(CW_BINARY) ? MF_UNCHECKED : MF_CHECKED);
  458.                break; 
  459.                 
  460.                 
  461.             } /* WM_COMMAND */
  462.  
  463.     default:
  464.           return(DefWindowProc(hWnd, message, wParam, lParam));
  465.     }
  466. return TRUE;
  467. } /* Boucle Windows */
  468.  
  469.  
  470.  
  471.  
  472. /* ******************************************************************* */
  473. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  474.                     LPSTR lpszCmdLine, int cmdShow)
  475. {
  476. MSG   msg;
  477.     // Go init this application, Change default Font
  478.     FirstEmission = (hPrevInstance==NULL);
  479.     
  480.     InitWindow (hInstance, hPrevInstance, lpszCmdLine, cmdShow);
  481.     /* Get and dispatch messages for this applicaton.*/
  482.     while (GetMessage(&msg, NULL, 0, 0))
  483.       {
  484.         TranslateMessage(&msg);
  485.         DispatchMessage(&msg);                  
  486.       }
  487.     UnregisterClass ("DLLCW", hInst);  
  488.     UnregisterClass ("CW_Main", hInst);  
  489. return(msg.wParam);
  490. } /* WinMain */
  491.  
  492.  
  493. /* ******************************************************************* */
  494.  
  495. int PASCAL FAR InitWindow (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  496.                            LPSTR lpszCmdLine, int cmdShow)
  497. {
  498. WNDCLASS wndClass;
  499.  
  500.     hInst = hInstance;       /* save for use by window procs */
  501.     strcpy (szApplication, "CW_Main");
  502.  
  503.     if (!hPrevInstance)     /* if no previous instance, this is first */
  504.       {
  505.         /* Get string from resource with application name. */
  506.         /* Define the window class for this application. */
  507.         wndClass.lpszClassName = szApplication;
  508.         wndClass.hInstance     = hInstance;
  509.         wndClass.lpfnWndProc   = MainWndProc;
  510.         wndClass.hCursor       = LoadCursor(hInstance, IDC_ARROW);
  511.         wndClass.hIcon         = LoadIcon  (hInstance, "CW_MAIN");
  512.         wndClass.lpszMenuName  = "CW_MAIN";
  513.         wndClass.hbrBackground = 1 + COLOR_WINDOW;
  514.         // style : Redraw window if its vertcal size has been changed
  515.         wndClass.style         = CS_VREDRAW | CS_HREDRAW;
  516.         wndClass.cbClsExtra    = 0;
  517.         wndClass.cbWndExtra    = 0;
  518.      // Register the class
  519.         RegisterClass(&wndClass);
  520.       }
  521.     hwnd = CreateWindow(
  522.                     szApplication,     // window class name
  523.                     szApplication,     // window title
  524.                     WS_OVERLAPPEDWINDOW | WS_HSCROLL | WM_VSCROLL | WS_VISIBLE,
  525.                     CW_USEDEFAULT,          // x - same as dialog box
  526.                     CW_USEDEFAULT,          // y
  527.                     470,                     // cx
  528.                     250,                     // cy
  529.                     NULL,                    // no parent for this window
  530.                     NULL,                    // use the class menu
  531.                     hInstance,               // who created this window
  532.                     NULL                     // no parms to pass on
  533.           );
  534.     if (hwnd==NULL)  exit(0);
  535.     ShowWindow (hwnd, cmdShow);
  536.     UpdateWindow (hwnd);
  537. return 0;
  538. }  /* InitWindows */
  539.  
  540.  
  541.  
  542.  
  543.  
  544.